namespace Billing2
{
    using AjaxControlToolkit;
    using System;
    using System.Collections;
    using System.Data;
    using System.Reflection;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ucSearch : UserControl
    {
        protected UpdatePanel ActionPanel;
        protected Button btnReset;
        protected Button btnSearch;
        protected CalendarExtender CalendarExtender1;
        protected CalendarExtender CalendarExtender2;
        protected DropDownList ddlSearch;
        private DataTable dt = null;
        protected Label LblActive;
        protected Label LblFrom;
        protected Label LblTo;
        protected Panel PanelDate;
        protected Panel PanelDetails;
        protected Panel PanelSearch;
        protected RadioButtonList RblActive;
        protected TextBox txtFromDate;
        protected TextBox txtSearch;
        protected TextBox txtToDate;
        protected ucHighlight ucHighlight1;
        protected UpdatePanel updatePanel3;
        protected UpdateProgress updateprogress1;
        protected HiddenField xSearchData;

        private void BindData()
        {
            this.ddlSearch.DataSource = (Hashtable) this.ViewState["SearchCriteria"];
            this.ddlSearch.DataValueField = "Key";
            this.ddlSearch.DataTextField = "Value";
            this.ddlSearch.DataBind();
            if (this.ddlSearch.Items.Count > 0)
            {
                this.SetSearchData = "";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            this.FilterDT = null;
            this.txtSearch.Text = "";
            this.RblActive.SelectedIndex = 0;
            this.SetPanelDate(false);
            this.ucHighlight1.CloseAll();
            ClsUtilityHelper.ResetFormControlValues(this);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.SearchFunction();
        }

        protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetSearchData = this.ddlSearch.SelectedValue;
            if (this.ddlSearch.SelectedValue.ToLower().Contains("date"))
            {
                this.SetPanelDate(true);
            }
            else
            {
                this.SetPanelDate(false);
            }
        }

        public DataTable getData()
        {
            MethodInfo mi = this.Page.GetType().GetMethod("getData");
            this.dt = (DataTable) mi.Invoke(this.Page, null);
            return this.dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.SetSearchData = this.ddlSearch.SelectedValue;
                if (this.ddlSearch.SelectedValue.ToLower().Contains("date"))
                {
                    this.SetPanelDate(true);
                }
                else
                {
                    this.SetPanelDate(false);
                }
                this.BindData();
            }
        }

        protected void RblActive_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SearchFunction();
        }

        private void SearchFunction()
        {
            this.ucHighlight1.CloseAll();
            DataView dv = new DataView(this.getData());
            if (dv.Count != 0)
            {
                if (this.PanelDate.Visible && ((this.txtFromDate.Text == "") || (this.txtToDate.Text == "")))
                {
                    this.ucHighlight1.ErrorMessage = "Search Period required";
                }
                else if (((this.PanelDate.Visible && (this.txtFromDate.Text != "")) && (this.txtToDate.Text != "")) && (Convert.ToDateTime(this.txtFromDate.Text, ClsApplicationConfiguration.CurrentCultureInfo).CompareTo(Convert.ToDateTime(this.txtToDate.Text, ClsApplicationConfiguration.CurrentCultureInfo)) == 1))
                {
                    this.ucHighlight1.ErrorMessage = "Invalid From Date";
                }
                else
                {
                    string filterStr = "";
                    string key = this.ViewState["SearchBy"] != null ? this.ViewState["SearchBy"].ToString() : "";
                    string value = this.txtSearch.Text.ToString().Trim();
                    if (key != "" && !Common.ValidFieldName(key))
                    {
                        EPPLog.Logger.Debug("invalid char in seach string: '" + key + "'");
                        return;
                    }
                    if (value != "" && !Common.ValidSeachValue(value))
                    {
                        EPPLog.Logger.Debug("invalid char in seach string: '" + value + "'");
                        return;
                    }

                    if (this.PanelDate.Visible)
                    {
                        filterStr = string.Format("Convert(" + key + ", 'System.DateTime') >= #{0:MM/dd/yyyy}# AND Convert(" + value + ", 'System.DateTime') < #{1:MM/dd/yyyy}#", Convert.ToDateTime(this.txtFromDate.Text, ClsApplicationConfiguration.CurrentCultureInfo), Convert.ToDateTime(this.txtToDate.Text, ClsApplicationConfiguration.CurrentCultureInfo).AddDays(1.0));
                    }
                    if (this.txtSearch.Text.Trim() != "")
                    {
                        filterStr = "Convert (" + key + ", 'System.String') Like '%" + value + "%'";
                    }
                    if (filterStr.Trim() != "")
                    {
                        dv.RowFilter = filterStr;
                        this.dt = dv.ToTable();
                    }
                    try
                    {
                        dv = new DataView(this.dt);
                        if (this.RblActive.SelectedValue.ToLower() == "1")
                        {
                            filterStr = "RecordStatus='Active'";
                        }
                        else if (this.RblActive.SelectedValue.ToLower() == "2")
                        {
                            filterStr = "RecordStatus='InActive'";
                        }
                        dv.RowFilter = filterStr;
                        this.dt = dv.ToTable();
                    }
                    catch (TimeoutException e2)
                    {
                        EPPLog.Logger.Error(e2);
                    }
                    catch (Exception ex)
                    {
                        EPPLog.Logger.Error(ex);
                    }
                    if (this.dt.Rows.Count == 0)
                    {
                        this.ucHighlight1.ErrorMessage = " Search Records not found. ";
                    }
                }
            }
        }

        private void SetActive(bool flag)
        {
            this.LblActive.Visible = true;
            this.RblActive.Visible = true;
        }

        private void SetPanelDate(bool flag)
        {
            this.PanelSearch.Visible = !flag;
            this.PanelDate.Visible = flag;
        }

        public DataTable FilterDT
        {
            get
            {
                if (this.dt == null)
                {
                    this.SearchFunction();
                }
                return this.dt;
            }
            set
            {
                this.dt = value;
            }
        }

        public Hashtable SearchCriteria
        {
            set
            {
                this.ViewState["SearchCriteria"] = value;
                this.PanelDetails.Visible = true;
                this.BindData();
            }
        }

        public Hashtable SearchCriteriaSubscriber
        {
            set
            {
                this.SearchCriteria = value;
                this.SetActive(true);
            }
        }

        public string SetSearchData
        {
            get
            {
                return this.ViewState["SearchBy"].ToString();
            }
            set
            {
                this.ViewState["SearchBy"] = value;
                if ((value == "") && (this.ddlSearch.Items.Count > 0))
                {
                    this.ViewState["SearchBy"] = this.ddlSearch.SelectedValue.ToString();
                }
                this.ddlSearch.SelectedIndex = -1;
                if (this.ViewState["SearchBy"].ToString() != "")
                {
                    this.ddlSearch.Items.FindByValue(this.ViewState["SearchBy"].ToString()).Selected = true;
                }
            }
        }
    }
}
